/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.Teacher;
import me.zhengjie.modules.anniversary.repository.TeacherRepository;
import me.zhengjie.modules.anniversary.service.TeacherService;
import me.zhengjie.modules.anniversary.service.dto.TeacherDto;
import me.zhengjie.modules.anniversary.service.dto.TeacherQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.TeacherMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class TeacherServiceImpl implements TeacherService {

    private final TeacherRepository teacherRepository;
    private final TeacherMapper teacherMapper;

    @Override
    public Map<String,Object> queryAll(TeacherQueryCriteria criteria, Pageable pageable){
        Page<Teacher> page = teacherRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(teacherMapper::toDto));
    }

    @Override
    public List<TeacherDto> queryAll(TeacherQueryCriteria criteria){
        return teacherMapper.toDto(teacherRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TeacherDto findById(Long id) {
        Teacher teacher = teacherRepository.findById(id).orElseGet(Teacher::new);
        ValidationUtil.isNull(teacher.getId(),"Teacher","id",id);
        return teacherMapper.toDto(teacher);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TeacherDto create(Teacher resources) {
        return teacherMapper.toDto(teacherRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Teacher resources) {
        Teacher teacher = teacherRepository.findById(resources.getId()).orElseGet(Teacher::new);
        ValidationUtil.isNull( teacher.getId(),"Teacher","id",resources.getId());
        teacher.copy(resources);
        teacherRepository.save(teacher);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            teacherRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TeacherDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TeacherDto teacher : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", teacher.getUserId());
            map.put("名称", teacher.getName());
            map.put("性别", teacher.getGender());
            map.put("出生年月", teacher.getBirthDate());
            map.put("入职年月", teacher.getWorkDate());
            map.put("民族ID", teacher.getNationId());
            map.put("政治面貌ID", teacher.getPoliticalId());
            map.put("职称", teacher.getPostitional());
            map.put("职务", teacher.getJob());
            map.put("目前是否在贵州民大工作", teacher.getInGzmu());
            map.put("行政部门ID", teacher.getServiceDepartId());
            map.put("学院ID", teacher.getAcademyDepartId());
            map.put("系部/科室ID", teacher.getFacultyDepartId());
            map.put("目前所从事行业", teacher.getIndustry());
            map.put("工作单位", teacher.getWorkUnit());
            map.put("身份证号码", teacher.getIdCardNo());
            map.put("联系电话", teacher.getPhone());
            map.put("QQ号", teacher.getQq());
            map.put("微信号", teacher.getWeixin());
            map.put("简介", teacher.getBrief());
            map.put("认证状态（0：未认证，1：已认证）", teacher.getVerifyStatus());
            map.put("创建者名称", teacher.getCreateBy());
            map.put("创建时间", teacher.getCreateTime());
            map.put("修改者名称", teacher.getUpdatedBy());
            map.put("修改时间", teacher.getUpdateTime());
            map.put("排序", teacher.getSort());
            map.put("备注", teacher.getRemark());
            map.put("是否启用", teacher.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}